from config.mysql_config import get_db
from dao.ppt_dao import update_ppt_state, get_ppt_by_name, add_ppt, update_ppt_fields, get_ppt_by_id
from utils.log import logger
from pojo.ppt import PPT


def create_new_ppt(ppt: PPT):
    """
    创建PPT
    :param ppt: PPT对象
    :return: 新的PPT
    """
    with get_db() as db:
        ppt = add_ppt(db, ppt)
        return ppt.to_dict()

def update_ppt_state_service(ppt_id, ppt_state):
    """
    修改PPT状态
    :param ppt_id:
    :param ppt_state:
    :return:
    """
    logger.info(f"修改PPT状态，此时PPT状态为:{ppt_state}")
    with get_db() as db:
        data = update_ppt_state(db, ppt_id, ppt_state)
        return data.to_dict()


def get_ppt_by_name_service(pptx_name):
    """
    通过具体名字查询PPT，不是模糊查询
    :param pptx_name:具体名字
    :return:一个PPT内容
    """
    with get_db() as db:
        ppt = get_ppt_by_name(db, pptx_name)
        return ppt.to_dict()

def update_ppt_fields_service(ppt_id, update_data):
    """
    修改PPT表的字段的值
    :param ppt_id: PPTid
    :param update_data: 待修改的数据，是json键值对，类似于：{"ppt_image_path": first_value}
    :return: 修改成功后的PPT表格数据
    """
    logger.info(f"修改PPT字段数据：{update_data}")
    with get_db() as db:
        ppt = update_ppt_fields(db,ppt_id, update_data)
        return ppt.to_dict()



def get_ppt_by_id_service(ppt_id):
    """
    通过id拿到PPT
    :param ppt_id:
    :return: ppt对象
    """
    with get_db() as db:
        ppt = get_ppt_by_id(db,ppt_id)
        return ppt.to_dict()